package com.lagou.demo

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

object SearchTable {


  def main(args: Array[String]): Unit = {
    // 创建SparkContext
    val conf = new SparkConf().setAppName(this.getClass.getCanonicalName.init).setMaster("local[*]")
    val sc = new SparkContext(conf)
    sc.setLogLevel("WARN")


    val srcFile0 = "file:///G:\\ysj\\client\\TsProj\\src\\game\\*";
    val srcFile1 = "file:///G:\\ysj\\client\\TsProj\\src\\game\\*\\*.ts";
    val srcFile2 = "file:///G:\\ysj\\client\\TsProj\\src\\game\\*\\*\\*.ts";
    val srcFile3 = "file:///G:\\ysj\\client\\TsProj\\src\\game\\*\\*\\*\\*.ts";
    val srcFile4 = "file:///G:\\ysj\\client\\TsProj\\src\\game\\*\\*\\*\\*\\*.ts";
    val srcFile5 = "file:///G:\\ysj\\client\\TsProj\\src\\game\\*\\*\\*\\*\\*\\*.ts";
    val srcFile6 = "file:///G:\\ysj\\client\\TsProj\\src\\game\\*\\*\\*\\*\\*\\*\\*.ts";
    val fileList = Array(srcFile0, srcFile1, srcFile2, srcFile3, srcFile4, srcFile5, srcFile6);
    val content_map: RDD[(String, String)] = sc.wholeTextFiles(fileList.mkString(","))

    val arr_lines: Array[String] = content_map.collect().map {
      content =>
        //println(s"path = ${content._1}")
        content._2
    }.flatMap(_.split("\n+"))

    /**
     * path = file:/G:/ysj/client/TsProj/src/game/entity/component/BagComponent.ts
     * path = file:/G:/ysj/client/TsProj/src/game/entity/component/BattleArrayComponent.ts
     * path = file:/G:/ysj/client/TsProj/src/game/entity/component/CardBaoQiComponent.ts
     */
    //arr_lines.take(5).foreach(println(_))
    arr_lines.take(5).foreach { l => println(s"每一行==> ${l}") }

    /**
     * 每一行==> import { Component } from "../../../framework/entity/Component";
     * 每一行==> import { ysj_ts } from "../../../data/pb/gen/pb";
     * 每一行==> import { S } from '../../../global/GameConfig';
     * 每一行==> import { itemconfigTR } from "data/fb/itemconfig-t-r";
     * 每一行==> import { ArrayMap } from "framework/common/ArrayMap";
     */
    println(s"行数 = ${arr_lines.length}");
    /**
     * 行数 = 68243
     */

    //用分隔符处理【空格，点，等于号，分号】
    val words = arr_lines.flatMap(_.trim.split("\\s+|\\.|=|;"))
    words.take(3).foreach { w => println(s"每单词==> ${w}") }

    /**
     * 每单词==> import
     * 每单词==> {
     * 每单词==> Component
     */
    println(s"单词数 = ${words.length}");
    /**
     * 单词数 = 230893
     */
    //过滤关键词前缀
    val filerStartWord = words.filter(_.startsWith("get"))
    println(s"滤前缀==> ${filerStartWord.length}");

    /**
     * 滤前缀==> 5664
     */
    filerStartWord.take(3).foreach { w => println(s"每前缀==> ${w}") }
    /**
     * 每前缀==> getInstance()
     * 每前缀==> getInstance()
     * 每前缀==> getInstance()
     */
    //过滤关键词后缀
    val filerEndWord = words.filter(_.endsWith("TBS()"))
    println(s"滤后缀==> ${filerEndWord.length}");

    /** 滤后缀==> 197 */
    filerEndWord.foreach { w => println(s"每后缀==> ${w}") }

    /**
     * 每后缀==> getmainlevelconfigTBS()
     * 每后缀==> getmainlevelsettingconfigTBS()
     * 每后缀==> getelementalresonanceconfigTBS()
     */


    val listFile = "file:///C:\\Users\\pc\\Desktop\\table_list.txt";
    val listData = sc.textFile(listFile).map(x => (x.split("\\|")(0))).collect();
    listData.foreach(println)

    //广播变量
    val listBC = sc.broadcast(listData)
    //Join,计数,筛选,去重
    val tuples = filerEndWord.map { iter =>
      val tabInfo: Array[String] = listBC.value
      //println(iter)
      var cnt = 0;
      for (tab <- tabInfo) {
        if (iter.toLowerCase.contains("get" + tab.toLowerCase + "tbs")) {
          cnt = cnt + 1;
        }
      }
      (iter, cnt);
    }.filter(_._2 > 0).distinct

    tuples.take(3).foreach(println(_))
    /**
     * (getleveldifficultyconfigTBS(),1)
     * (getmonsterviewconfigTBS(),1)
     */

    println(s"匹配 = ${tuples.length}")
    /**
     * 匹配 = 2
     */

    // 关闭SparkContext
    sc.stop()
  }
}

